package com.example.leero.graduation.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.leero.graduation.API;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.adapter.ManageAdapter;
import com.example.leero.graduation.model.ApplyItem;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class ManageActivity extends AppCompatActivity {

    private RecyclerView rv_manage;
    private List<ApplyItem> applyList = new ArrayList();
    private ManageAdapter adapter;
    private int act_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);

        Intent intent = getIntent();
        act_id = intent.getIntExtra("act_id", 0);
        setupRv();

        getApplyList();

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("活动管理");
        findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setupRv() {
        rv_manage = (RecyclerView) findViewById(R.id.rv_manage);
        rv_manage.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ManageAdapter(this, R.layout.item_apply, applyList);
        rv_manage.setAdapter(adapter);
    }

    private void getApplyList() {
        OkHttpUtils.post().url(API.GET_APPLY_LIST).addParams("act_id", act_id+"").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                applyList.clear();
                applyList.addAll(GsonUtil.jsonToArrayList(GsonUtil.getData(response), ApplyItem.class));
                adapter.notifyDataSetChanged();
            }
        });
    }
}
